package com.hedl.practice.diyJava.reflex.method_access;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;

//反射在多态中的应用和访问流程
public class Main {

    /**
     * 使用反射调用方法时，仍然遵循多态原则：即总是调用实际类型的覆写方法（如果存在）
     * @param args
     */
    public static void main(String[] args) {
        try {
            Method m = Persons.class.getMethod("hello");
            m.invoke(new Students());
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

}

class Persons{
    public void hello(){
        System.out.println("Persons:hello");
    }
}

class Students extends Persons{
    public void hello(){
        System.out.println("Students:hello");
    }
}